package cn.itsource.service.impl;

import cn.itsource.constants.Constants;
import cn.itsource.constants.GlobalExceptionCode;
import cn.itsource.exception.GlobalException;
import cn.itsource.mapper.DriverContractMapper;
import cn.itsource.mapper.DriverMapper;
import cn.itsource.pojo.domain.Driver;
import cn.itsource.pojo.domain.DriverContract;
import cn.itsource.service.IDriverContractService;
import cn.itsource.template.OssTemplate;
import cn.itsource.utils.BitStatesUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beishan
 * @since 2024-04-11
 */
@Service
@Slf4j
public class DriverContractServiceImpl extends ServiceImpl<DriverContractMapper, DriverContract> implements IDriverContractService {

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private OssTemplate ossTemplate;
    /**
     * 将 司机合同状态改为作废
     * @param id
     */

    @Override
    public void changeStatus(Long id) {
        Date date = new Date();
        DriverContract contract = super.getById(id);
        contract.setDriverContractStatus(Constants.Contract.CANCEL);
        contract.setUpdateTime(date);
        super.updateById(contract);

        //维护driver表的位状态
        Driver driver = driverMapper.selectById(contract.getDriverId());
        //判断是否 签署协议
        if(!BitStatesUtil.hasState(driver.getBitState(),BitStatesUtil.OP_DRIVER_AGREEMENT)){
            throw new GlobalException(GlobalExceptionCode.DRIVER_CONTRACT_ERROR);
        }

        //修改位状态
        driver.setBitState(BitStatesUtil.removeState(driver.getBitState(), BitStatesUtil.OP_DRIVER_AGREEMENT));
        driver.setUpdateTime(date);
        driverMapper.updateById(driver);
    }

    /**
     * 文件上传 并将文件 读取文件信息
     * @param uploadFile
     */
    @Override
    @GlobalTransactional(name = "driver-contract-upload", rollbackFor = Exception.class)
    public String upload(MultipartFile uploadFile) {

//        ArrayList<String> list = new ArrayList<>();
//        String text = "";
//        String substring = uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf("."));
//        //获取文件内容
//        try {
//            InputStream inputStream = uploadFile.getInputStream();
//
//            if (substring.equals("doc")){
//                WordExtractor extractor = new WordExtractor(inputStream);
//                text = extractor.getText();
//                list.add(text);
//            }else if (substring.equals("docx")){
//                XWPFDocument document = new XWPFDocument(inputStream);
//                XWPFWordExtractor extractor = new XWPFWordExtractor(document);
//                text = extractor.getText();
//                list.add(text);
//            }
//            log.info("文件内容 ---{}",list);
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new GlobalException(GlobalExceptionCode.FILE_UPLOAD_ERROR);
//        }


        //协议地址
        String upload = ossTemplate.upload(uploadFile, Constants.Contract.Dir);
        //地址保存到数据库
       return upload;


    }




}
